package tool

import (
	"crypto/md5"
	"encoding/hex"
	"fmt"
	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"
	"os"
	"strings"
)


func Md5Str(str string) string{
	h := md5.New()
	h.Write([]byte(str))
	hex.EncodeToString(h.Sum(nil))
	return hex.EncodeToString(h.Sum(nil))
}


func SessionSet(c *gin.Context,key string,value interface{}){
	session := sessions.Default(c)
	session.Set(key,value)
	session.Save()
}

func SessionGet(c *gin.Context,key string) interface{}{
	session := sessions.Default(c)
	return session.Get(key)
}

func SessionDel(c *gin.Context,key string){
	session := sessions.Default(c)
	session.Delete(key)
}

//多个sql 语句合并 map

func MapWhere(field string,exp string,val interface{}) []map[string]interface{}{
    maps := []map[string]interface{}{}
    maps = append(maps, map[string]interface{}{
		"filed":field,
		"exp":exp,
		"val":val,
	})
    return maps
}

func HttpBuildQuery(params map[string]string) (param_str string) {
	params_arr := make([]string, 0, len(params))
	for k, v := range params {
		params_arr = append(params_arr, fmt.Sprintf("%s=%s", k, v))
	}
	param_str = strings.Join(params_arr, "&")
	return param_str
}

func PathExists(path string) (bool, error) {
	_, err := os.Stat(path)
	if err == nil {
		return true, nil
	}
	if os.IsNotExist(err) {
		return false, nil
	}
	return false, err
}



